//
//  WSPlayerDetailViewController.m
//  CharonTVProject
//
//  Created by karong on 2023/8/8.
//

#import "WSPlayerDetailViewController.h"
#import "GKSlidePopupView.h"
#import "GKBallLoadingView.h"
#import "GKLikeView.h"
#import "GKDYVideoScrollView.h"
#import "GKDYVideoCell.h"
#import "GKDYPlayerManager.h"
#import "GKDYVideoPortraitCell.h"
#import "WSPlayerRequest.h"
#import "WSVideoDetail.h"
#import "CTMediator+WSPlayerAction.h"
#import "CTMediator+WSPayAction.h"
#import "WSPayController.h"
#import "WSCoinsPayController.h"
#import "WSVipPayController.h"
#import "CTMediator+WSUserCenterAction.h"
#import "WSPayRequest.h"

@interface WSPlayerDetailViewController () <GKDYPlayerManagerDelegate, WSCoinsPayControllerDelegate, WSPayControllerDelegate>
@property (nonatomic, strong) GKDYPlayerManager * manager;
@property (nonatomic, strong) WSVideoDetail * videoDetail;
@property (strong, nonatomic) WSPayController *payVC;
@property (strong, nonatomic) WSCoinsPayController *coinVC;
@property (strong, nonatomic) WSVipPayController *vipVC;
@end

@implementation WSPlayerDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [self addObserver];
    [self loadTVDetailcharonTV_Request];
}
- (void)addObserver {
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loadTVDetailcharonTV_Request) name:WSVipChangeNotification object:nil];
}

- (void)dealloc {
    NSLog(@"playerVC dealloc");
}

- (void)initUI {
    self.view.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.manager.scrollView];
    [self.manager.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

- (void)loadTVDetailcharonTV_Request {
    WeakSelf
    [WSPlayerRequest loadcharonTV_shotTVDetailWithID:self.charonTV_video.tv_id success:^(id obj) {
        self.videoDetail = obj;
        if ([self.delegate respondsToSelector:@selector(loadVideoDetailSuccess:)]) {
            [self.delegate loadVideoDetailSuccess:obj];
        }
        [weakSelf.manager.dataSources addObjectsFromArray:self.videoDetail.chapter_list];
        [weakSelf removeManualPayment];
        [weakSelf.manager playVideoWithVideo:weakSelf.charonTV_video];
        [weakSelf.manager.scrollView.mj_header endRefreshing];
        [weakSelf.manager.scrollView.mj_footer endRefreshing];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        
    }];
}

- (void)likecharonTV_Request:(WSVideo *)model {
    [WSPlayerRequest favoritecharonTV_WithChapter_id:model.id tv_id:model.tv_id success:^{
        model.isLike = YES;
        model.like_num ++;
        if (self.model.id == model.id) {
            self.manager.currentCell.portraitView.model = model;
        }
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        
    }];
}

- (void)payVideo:(WSVideo *)model {
    WeakSelf
    [WSPayRequest payWithChapter_id:model.id success:^(NSString *string) {
        weakSelf.model.url = string;
        [weakSelf coinsPayControllerReloadAction:model];
        [WSToast simple:APPLocalized(@"餘額充足，已為您自動購買本集")];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        
    }];
}

//解锁弹窗
- (void)unlockAlter:(WSVideo *)model {
    if (model.is_vip) {
        ///vip彈窗
        self.manager.scrollView.scrollEnabled = NO;
        [self removeManualPayment];
        self.vipVC = [[CTMediator sharedInstance]ct_showcharonTV_Vip];
        self.vipVC.charonTV_video = model;
        [self.view addSubview:self.vipVC.view];
        [self addChildViewController:self.vipVC];
        [self.vipVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_offset(0);
            make.bottom.left.right.mas_equalTo(self.view);
        }];
    }
    else if (appConfig.isAutoPay && appUserCenter.coins >= model.coins) {
        [self payVideo:model];
    }
    else if (model.coins > appUserCenter.coins) {
        self.manager.scrollView.scrollEnabled = NO;
        [self removeManualPayment];
        self.coinVC = [[CTMediator sharedInstance]ct_showcharonTV_Coin];
        self.coinVC.charonTV_video = model;
        self.coinVC.delegate = self;
        [self.view addSubview:self.coinVC.view];
        [self addChildViewController:self.coinVC];
        [self.coinVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_offset(0);
            make.bottom.left.right.mas_equalTo(self.view);
        }];
    }
    else {
        self.manager.scrollView.scrollEnabled = NO;
        [self removeManualPayment];
        self.payVC = [[CTMediator sharedInstance]ct_showcharonTV_Pay];
        self.payVC.charonTV_video = model;
        self.payVC.delegate = self;
        [self.view addSubview:self.payVC.view];
        [self addChildViewController:self.payVC];
        [self.coinVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_offset(0);
            make.bottom.left.right.mas_equalTo(self.view);
        }];
    }
}

- (void)removeManualPayment {
    if (self.payVC) {
        self.manager.scrollView.scrollEnabled = YES;
        [self.payVC removeFromParentViewController];
        [self.payVC.view removeFromSuperview];
        self.payVC = nil;
    }
    if (self.coinVC) {
        self.manager.scrollView.scrollEnabled = YES;
        [self.coinVC removeFromParentViewController];
        [self.coinVC.view removeFromSuperview];
        self.coinVC = nil;
    }
    if (self.vipVC) {
        self.manager.scrollView.scrollEnabled = YES;
        [self.vipVC removeFromParentViewController];
        [self.vipVC.view removeFromSuperview];
        self.vipVC = nil;
    }
}

- (WSVideo *)model {
    return self.manager.currentCell.model;
}

-(void)openChapterMenu {
    [self openChapterMenuWithModel:self.model];
}

- (void)openChapterMenuWithModel:(WSVideo *)model {
    WeakSelf
    UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_ChapterListWithVideoDetail:self.videoDetail video:model selectHandle:^(id obj) {
        [weakSelf removeManualPayment];
        [weakSelf.manager playVideoWithVideo:obj];
    }];
    target.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    target.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:target animated:YES completion:nil];
}
#pragma mark - JXCategoryListContentViewDelegate
- (UIView *)listView {
    return self.view;
}

- (void)listDidAppear {
    self.manager.isAppeared = YES;
    [self.manager play];
}

- (void)listWillDisappear {
    [self.manager pause];
}

- (void)listDidDisappear {
    self.manager.isAppeared = NO;
}

#pragma mark - GKDYPlayerManagerDelegate

- (void)cellDidClickLike:(WSVideo *)model {
    [self likecharonTV_Request:model];
}

- (void)cellDidClickChapter:(WSVideo *)model {
    [self openChapterMenuWithModel:model];
}

- (void)cellDidScroll2Video:(WSVideo *)model {
    if (model.needPay) {
        [self unlockAlter:model];
    }
}

#pragma mark WSCoinsPayControllerDelegate、WSPayControllerDelegate
- (void)coinsPayControllerReloadAction:(id)object {
    [self removeManualPayment];
    [self.manager playVideoWithVideo:object];
}

- (void)coinsPayControllerMoreAction {
    UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_PurchaseVCWithIndex:1];
    [self.navigationController pushViewController:target animated:YES];
}
#pragma mark - 懒加载
- (GKDYPlayerManager *)manager {
    if (!_manager) {
        _manager = [[GKDYPlayerManager alloc] init];
        _manager.delegate = self;
    }
    return _manager;
}

@end
